package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsStandardSystem;
import com.ruoyi.system.domain.news.dto.NewsStandardSystemDto;
import com.ruoyi.system.domain.news.query.NewsStandardSystemQuery;
import com.ruoyi.system.domain.news.vo.NewsStandardSystemVo;
import com.ruoyi.system.service.news.NewsStandardSystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsStandardSystem")
@Api(value = "newsStandardSystemController", tags  = "标准编制")
public class NewsStandardSystemController extends BaseController {

	@Resource
	private NewsStandardSystemService newsStandardSystemService;

    @ApiOperation(value = "新增/修改标准编制信息", notes = "新增/修改标准编制信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsStandardSystemDto newsStandardSystemDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsStandardSystem newsStandardSystem = new NewsStandardSystem();
		BeanUtils.copyProperties(newsStandardSystemDto,newsStandardSystem);
		if (newsStandardSystem.getId()==null) {
			newsStandardSystem.setCreateBy(loginUser.getUsername());
			newsStandardSystem.setDeptId(loginUser.getDeptId());
			newsStandardSystem.setCreateTime(new Date());
			newsStandardSystem.setUpdateBy(loginUser.getUsername());
			newsStandardSystem.setUpdateTime(new Date());
		} else {
			newsStandardSystem.setUpdateBy(loginUser.getUsername());
			newsStandardSystem.setUpdateTime(new Date());
		}
		boolean result = newsStandardSystemService.saveOrUpdate(newsStandardSystem);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除标准编制信息", notes = "删除标准编制信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsStandardSystemService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个标准编制信息", notes = "获取单个标准编制信息")
	@GetMapping("/getById")
	public R<NewsStandardSystemVo> getById(@RequestParam(value = "id") Long id) {
		NewsStandardSystemQuery newsStandardSystemQuery = new NewsStandardSystemQuery();
		newsStandardSystemQuery.setId(id);
		List<NewsStandardSystemVo> newsStandardSystemList = newsStandardSystemService.getNewsStandardSystemList(newsStandardSystemQuery);
		if (newsStandardSystemList!=null&&newsStandardSystemList.size()>0) {
			return R.ok(newsStandardSystemList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取标准编制信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsStandardSystemVo>> list(@RequestBody NewsStandardSystemQuery newsStandardSystemQuery) {
		List<NewsStandardSystemVo> newsStandardSystemList = newsStandardSystemService.getNewsStandardSystemList(newsStandardSystemQuery);
		return R.ok(new PageInfo<>(newsStandardSystemList));
	}
}
